{% extends 'admin/common/base.html' %}
{% block body %}

<body class="gray-bg">
    <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="role-form">
                    <div class="select-list">
                        <ul>
                            <li>
                                名称：<input type="text" name="name" />
                            </li>
                            <li>
                                <label>类型：</label>
                                <select name="mtype">
                                    <option value="">所有</option>
                                    {% for dd in get_dict_data('cms_material_type')%}
                                    <option value="{{dd.value}}">{{dd.label}}</option>
                                    {% endfor %}
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                        class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                        class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success {{check_perms('cms:material:add')}}" onclick="$.operate.add()">
                    <i class="fa fa-plus"></i> 新增
                </a>
                <a class="btn btn-primary single disabled {{check_perms('cms:material:edit')}}"
                    onclick="$.operate.edit()">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled {{check_perms('cms:material:remove')}}"
                    onclick="$.operate.removeAll()">
                    <i class="fa fa-remove"></i> 删除
                </a>
            </div>

            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
</body>
{% endblock %}
{% block js %}
{{ super() }}
<script>
    var editFlag = "{{check_perms('cms:material:edit')}}";
    var removeFlag = "{{check_perms('cms:material:remove')}}";
    var mtype_datas = {{ get_dict_data('cms_material_type') | tojson | safe}};
    $(function () {
        var options = {
            url: "{{url_for('admin.cms.material.list')}}",
            createUrl: "{{url_for('admin.cms.material.add')}}",
            updateUrl: "{{url_for('admin.cms.material.edit')}}?id={id}",
            removeUrl: "{{url_for('admin.cms.material.remove')}}",
            exportUrl: "{{url_for('admin.cms.material.export')}}",
            modalName: "素材",
            columns: [
                {
                    checkbox: true
                },
                {
                    field: 'id',
                    title: 'ID',
                    visible: false
                },
                {
                    field: 'name',
                    title: '名称'
                },
                {
                    field: 'mtype',
                    title: '类型',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(mtype_datas, value);
                    }
                },
                {
                    field: 'url',
                    title: '文件',
                    formatter: function (value, row, index) {
                        if (row.mtype == 'pic') {
                            return $.table.imageView(value);
                        }
                        let html = "<a href='" + value + "' target='_blank'>下载</a>"
                        return html;
                    }
                },
                {
                    title: '路径',
                    formatter: function (value, row, index) {
                        return '<a onclick="copyText(\'' + row.url + '\')">复制</a>'
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a> ');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    function copyText(text) {
        var oInput = document.createElement('input');
        oInput.value = text;
        document.body.appendChild(oInput);
        oInput.select();
        document.execCommand("Copy");
        oInput.className = 'oInput';
        oInput.style.display = 'none';
    }

</script>
{% endblock %}